package adapletplugin.dialogs;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class NewMapDialog extends TitleAreaDialog
{
	private String mapFileName;
	private Text mapFileNameText;

	public NewMapDialog(Shell parentShell)
	{
		super(parentShell);
		mapFileName = "";
	}

	@Override
	public void create()
	{
		super.create();

		setTitle("Create a new Map");
		setMessage("Please enter the name that you want this map file to use.");
		getButton(IDialogConstants.OK_ID).setEnabled(false);
	}

	@Override
	protected Control createDialogArea(Composite parent)
	{
		GridLayout layout = new GridLayout();
		parent.setLayout(layout);

		Composite composite = new Composite(parent, SWT.NULL);
		layout = new GridLayout();
		layout.numColumns = 2;
		composite.setLayout(layout);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		Label label = new Label(composite, SWT.NULL);
		label.setText("Map Name");

		mapFileNameText = new Text(composite, SWT.BORDER);
		mapFileNameText.setText("");
		mapFileNameText.addModifyListener(createKeyListener());
		mapFileNameText.setLayoutData(new GridData(SWT.FILL, SWT.NULL, true, false));

		return parent;
	}

	private ModifyListener createKeyListener()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				if (mapFileNameText.getText().isEmpty())
				{
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				}
				else
				{
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				}
			}
		};
	}

	public String getMapFileName()
	{
		return mapFileName;
	}

	@Override
	protected void okPressed()
	{
		mapFileName = mapFileNameText.getText();
		super.okPressed();
	}

}
